package com.multi.thread.chapter03.demo10;

/**
 *	假死出现的主要原因是有可能连续唤醒同类
 */
public class Run {
	
	public static void main(String[] args) throws InterruptedException {
		String lock = new String("");
		
		Product product = new Product(lock);
		Custom custom = new Custom(lock);
		
		ThreadProduct[] threadProduct = new ThreadProduct[2];
		ThreadCustom[] threadCustom = new ThreadCustom[2];
		
		for (int i = 0; i < 2; i++) {
			threadProduct[i] = new ThreadProduct(product);
			threadProduct[i].setName("生产者" + (i + 1));
			
			threadCustom[i] = new ThreadCustom(custom);
			threadCustom[i].setName("消费者" + (i + 1));
			
			threadProduct[i].start();
			threadCustom[i].start();
		}
		
		Thread.sleep(100);
		//activeCount()返回线程组中活动线程的估计数
		Thread[] threadArray = new Thread[Thread.currentThread().getThreadGroup().activeCount()];
		//enumerate()把此线程组及其子组中的所有活动线程复制到制定线程组
		Thread.currentThread().getThreadGroup().enumerate(threadArray);
		for (int i = 0; i < threadArray.length; i++) {
			System.out.println(threadArray[i].getName() + "_" + threadArray[i].getState());
		}
	}

}
